<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="80px" @keyup.enter="submitHandle()">
			<el-form-item label="药品名称" prop="goodName">
				<el-input v-model="dataForm.goodName"></el-input>
			</el-form-item>
			<el-form-item label="药品规格" prop="goodSpec">
				<el-input v-model="dataForm.goodSpec"></el-input>
			</el-form-item>
			<el-form-item label="药品厂家" prop="goodSource">
				<el-input v-model="dataForm.goodSource"></el-input>
			</el-form-item>
			<el-form-item label="药品进价" prop="goodPrice">
				<el-input v-model="dataForm.goodPrice" type="number"></el-input>
			</el-form-item>
			<el-form-item label="生产日期" prop="goodTime">
				<el-date-picker
					v-model="dataForm.goodTime"
					type="datetime"
				/>
			</el-form-item>
			<el-form-item label="有效期" prop="timeArea">
				<el-input v-model="dataForm.timeArea"></el-input>
			</el-form-item>
			<el-form-item label="药品单位" prop="goodUnit">
				<el-input v-model="dataForm.goodUnit"></el-input>
			</el-form-item>
			<el-form-item label="销售价格" prop="salePrice">
				<el-input v-model="dataForm.salePrice" type="number"></el-input>
			</el-form-item>
			<el-form-item label="药品类型" prop="goodType">
				<el-select v-model="dataForm.goodType" style="width: 100%;">
					<el-option
					v-for="item in dataForm.typelist"
					:key="item.value"
					:label="item.label"
					:value="item.value"
					/>
				</el-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useGoodApi, useGoodSubmitApi } from '@/api/sys/good'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	goodType: '',
	goodSpec: '',
	goodName: '',
	goodSource:'',
	goodPrice:'',
	salePrice:'',
	orgId:'',
	goodUnit:'',
	goodTime:'',
	timeArea:0,
	typelist:[
		{
			value: '1',
    		label: '西药',
		},
		{
			value: '2',
    		label: '中药',
		}
	],
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''
	let orgid:any=localStorage.getItem('org');
	dataForm.orgId=orgid;
	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	// id 存在则为修改
	if (id) {
		getPost(id)
	}
}

const getPost = (id: number) => {
	useGoodApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	goodName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	goodSpec: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useGoodSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
